package com.usernet.product.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.usernet.product.comm.BaseDao;
import com.usernet.product.entity.Photo;
import com.usernet.product.entity.Software;
import com.usernet.product.utils.ProductConfig;

/**
 * 应用截图信息
 * 
 * @author wangshuaib
 * 
 */
public class PhotoDao extends BaseDao {

	/**
	 * 查询应用的所有截图
	 * 
	 * @param softId
	 *            应用ID
	 * @return 截图信息列表
	 */
	@SuppressWarnings("unchecked")
	public List<Photo> getAllBySoftId(int softId) {
		String sql = "from Photo where status !=" + ProductConfig.DIS_STATUS
				+ " and software.id=" + softId + " order by id";
		return getAllByHql(sql);

	}
	/** 精品推荐 */
	@SuppressWarnings("unchecked")
	public List<Software> getTops(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.icon) from Software as soft where soft.status!=? and soft.tops=2 order by soft.time desc";
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Software> getGames(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.icon) from Software as soft where soft.status!=? and soft.type.flag=?";
			hql += " and tops!=0 order by soft.time desc";
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, ProductConfig.FLAG_GAME);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Software> getSofts(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.icon) from Software as soft where soft.status!=? and soft.type.flag=?";
			hql += " and tops!=0 order by soft.time desc";
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, ProductConfig.FLAG_SOFT);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}
}
